package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import util.DBUtil;

public class PsNb {

	public static void main(String[] args) {
		// PreparedStatement在执行类似sql的时候有优势
		String sql = "insert into subject(subjectname,classhour,gradeid)values(?,?,?)";
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {

			ps = con.prepareStatement(sql);
			// 多条,批处理操作
			for (int i = 1; i <= 20; i++) {

				ps.setString(1, "java" + i);
				ps.setInt(2, 20 + i);
				ps.setInt(3, 2);
				ps.addBatch();// 加入批处理
			}
			int[] ar = ps.executeBatch();// 执行批处理
			System.out.println("返回了：" + Arrays.toString(ar));
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			DBUtil.closeAll(rs, ps, con);
		}
	}

}
